package com.star.common.oss.autoconfig;

import com.google.common.collect.Maps;
import com.star.common.oss.enums.OssTypeEnum;
import com.star.common.oss.factory.OssFactory;
import com.star.common.oss.properties.OssProperties;
import com.star.common.oss.service.OssClient;
import com.star.common.oss.service.impl.AliyunOssClient;
import com.star.common.oss.service.impl.AwsOssClient;
import com.star.common.oss.service.impl.HuaweiCloudOssClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.Objects;

/**
 * oss存储自动装配
 *
 * @author zhouhengzhe #公众号：小白的Java进阶之路
 * @version 1.0
 * @date 2023/6/27 11:55
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(OssProperties.class)
public class OssAutoConfiguration {

    private final OssProperties ossProperties;


    public OssAutoConfiguration(OssProperties ossProperties) {
        this.ossProperties = ossProperties;
    }

    @Bean
    public Map<String, OssClient> ossClientMap() {
        Map<String, OssClient> map = Maps.newHashMap();
        if (Objects.nonNull(ossProperties.getAliyun())) {
            // 添加阿里云oss
            AliyunOssClient aliyunSmsClient = new AliyunOssClient(ossProperties.getAliyun());
            map.put(OssTypeEnum.ALIYUN.getCode(), aliyunSmsClient);
        }
        if (Objects.nonNull(ossProperties.getHuaweicloud())) {
            // 添加AWS oss
            HuaweiCloudOssClient huaweiCloudOssClient = new HuaweiCloudOssClient(ossProperties.getHuaweicloud());
            map.put(OssTypeEnum.HUAWEIYUN.getCode(), huaweiCloudOssClient);
        }
        if (Objects.nonNull(ossProperties.getAws())) {
            // 添加AWS oss
            AwsOssClient awsOssClient = new AwsOssClient(ossProperties.getAws());
            map.put(OssTypeEnum.AWS.getCode(), awsOssClient);
        }
        return map;
    }

    /**
     * 构建oss工厂
     *
     * @param ossClientMap oss服务端
     * @return oss工厂
     */
    @Bean
    public OssFactory ossFactory(Map<String, OssClient> ossClientMap) {
        return new OssFactory(ossClientMap);
    }
}
